package geo.com.geosdkdemo.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import geo.com.geosdk.GeoManage;
import geo.com.geosdk.listener.DeviceCommandSendListener;
import geo.com.geosdkdemo.R;

public class HostRegisterActivity extends Activity implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private EditText etRegisterCode;
    private Button btnRegister;
    private LinearLayout llProgress;
    private TextView tvNote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_host_register);
        initView();
        initData();
    }

    private void initData() {
        tvCommonTitle.setText("仪器注册");
    }

    private void initView() {
        ivCommonBack=findViewById(R.id.iv_common_back);
        tvCommonTitle=findViewById(R.id.tv_common_title);
        etRegisterCode=findViewById(R.id.et_register_code);
        btnRegister=findViewById(R.id.btn_register);
        llProgress=findViewById(R.id.layout_progress);
        tvNote=findViewById(R.id.text_note);

        ivCommonBack.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            finish();
        }else if(v==btnRegister){
            onClickBtnDetermine();
        }
    }

    private void onClickBtnDetermine() {
        if (etRegisterCode.getText().toString().isEmpty()) {
            makeTextString("注册码不能为空，请重新输入",Gravity.CENTER);
            return;
        }

        showProgressDialog(true);
        //2.监听发送状态
        GeoManage.getInstance().setDeviceCommandSendListener(new DeviceCommandSendListener() {
            @Override
            public void OnDeviceCommandSendCallBack(final int status, final String strFunction) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(status==4){
                            makeTextString("仪器注册失败!",Gravity.CENTER);
                            showProgressDialog(false);
                        }else if(status==5){
                            makeTextString("仪器注册成功!",Gravity.CENTER);
                            showProgressDialog(false);
                            finish();
                        }
                    }
                });

            }
        });


        //开始设置命令
        String strRegisterCode = etRegisterCode.getText().toString();
        strRegisterCode = strRegisterCode.replaceAll("-", "");
        GeoManage.getInstance().setDeviceRegister(strRegisterCode);

    }

    protected void makeTextString(String toastStr, int gravity) {
        Toast toast = Toast.makeText(this,toastStr, Toast.LENGTH_SHORT);
        if (Gravity.NO_GRAVITY != gravity) {
            toast.setGravity(gravity, 0, 0);
        }
        toast.show();
    }

    protected void showProgressDialog(boolean show){
        if(show){
            llProgress.setVisibility(View.VISIBLE);
        }else{
            llProgress.setVisibility(View.GONE);
        }
    }
}